<?php

/**
 * Description of server_detail
 *
 * @author Alex
 */
class server_detail {

    var $cached_file;
    var $is_online = false;
    var $requires_session = false;
    var $rFq = false;

    function server_detail($port)
    {

        include_once('includes/cache.php');
        include_once('includes/addMessage.php');

        global $db, $lang, $skin_dir, $properties;

        $cache = new cache('ServerDetail'.$port,5);
        $this->cached_file = $cache->filename;

        if(! $cache->valid) {

            include_once('includes/rfquery/rFQuery.php');
            include_once('includes/rfquery/rfqdisplayhelper.php');
            include_once('includes/rfquery/rfdriverinfo.php');

            $this->rFq = new rFQuery($properties['rfactor_serverip'], $port, false);
            $this->rFq->SetHideServerPrefix($properties['rfactor_hideserver']);

            $this->is_online = $this->rFq->_queryServerInfo(1);

            if ($this->is_online)
            {
               $session_names = array (
                    SESSION_PRACTICE1 => $lang['server_session_practice1'],
                    SESSION_PRACTICE2 => $lang['server_session_practice2'],
                    SESSION_PRACTICE3 => $lang['server_session_practice3'],
                    SESSION_PRACTICE4 => $lang['server_session_practice4'],
                    SESSION_QUALIFY  => $lang['server_session_qualify'],
                    SESSION_WARMUP   => $lang['server_session_warmup'],
                    SESSION_RACE     => $lang['server_session_race']
                );

		$rfqdh = new rFQDisplayHelper($this->rFq, $session_names, $lang);
                
                $server_name = $rfqdh->servername;
                $server_ip = "<a hRef=$rfqdh->serverURL>$rfqdh->fullsrvaddr</a>";
                $server_motd = $rfqdh->motd;
                $server_series = $rfqdh->series;
                $server_track = $rfqdh->track;
                $server_session = $rfqdh->session_info;
                $server_drivers = $rfqdh->drivers;


                if($this->rFq->hasextrainfo)
                {
                    $includestart = "";
                    $includeend = "";
                    $rowcount = 10;

                    $champ_name =  "<a href='championships.php?id={$this->rFq->champ_id}'>{$this->rFq->champ_name}</a>";
                    $event_name = "<a href='events.php?mode=entrants&id={$this->rFq->event_id}'>{$this->rFq->event_title}</a>";
                    $event_date = $this->rFq->event_date;

                    if($this->rFq->champ_broadcast != "")
                        $event_date = "<a href='{$this->rFq->champ_broadcast}'>{$lang['ChampionshipBroadcastAt']}{$event_date}</a>";

                    if($this->rFq->event_open)
                        $event_name .= " (<a class='openText' href='events.php?mode=entrants&id={$this->rFq->event_id}'>{$lang['EventActive']}</a>)";

                    $server_track = $this->rFq->track_name . '<br/>' . $this->rFq->track_location;
                    if($this->rFq->champ_logo != "")
                        $champ_logo = "<a href='championships.php?id={$this->rFq->champ_id}'><img src='{$this->rFq->champ_logo}' alt=''></a>";
                    else
                        $champ_logo = "";

                    if($this->rFq->track_logo != "")
                        $track_logo = "<a href='events.php?id={$this->rFq->event_id}'><img src='{$this->rFq->track_logo}' alt=''></a>";
                    else
                        $track_logo = "";
                }
                else
                {
                    $track_logo = "";
                    $champ_logo = "";

                    $includestart = "<!--";
                    $includeend = "-->";
                    $rowcount = 7;

                    $champ_name = "";
                    $event_name = "";
                    $event_date = "";
                }

                include($skin_dir . 'tpl/serverdetail.tpl');

                if($this->rFq->_getCurPlayers() > 0)
                {
                    $rFqDi = new rFDriverInfo($properties['rfactor_serverip'], $port + 257, false);
                    $result = $rFqDi->_queryDriverInfo();
                    if ($result)
                    {
                        if (RFPQDI_SESSION_RACE > $rFqDi->cur_session)
                        {
                            include($skin_dir . 'tpl/tableStart.tpl');
                            include($skin_dir . 'tpl/serverPracHead.tpl');

                            $on = true;
                            foreach ($rFqDi->drivers as $driver)
                            {
                                $mode = $on ? 'on' : 'off' ;
                                $on = !$on;
                                include($skin_dir . 'tpl/serverPracEntry.tpl');
                            }

                            include($skin_dir . 'tpl/tableEnd.tpl');
                        }
                        else
                        {
                            include($skin_dir . 'tpl/tableStart.tpl');
                            include($skin_dir . 'tpl/serverRaceHead.tpl');

                            $on = true;
                            foreach ($rFqDi->drivers as $driver)
                            {
                                $mode = $on ? 'on' : 'off' ;
                                $on = !$on;
                                include($skin_dir . 'tpl/serverRaceEntry.tpl');
                            }

                            include($skin_dir . 'tpl/tableEnd.tpl');
                        }
                    }
                }

            }
            else
            {
                $mess = new addMessage($lang['server_is_offline'],$lang['ErrorTitle']);
                echo '<!--OFF-->';
                $mess->render();
            }

            $cache->writeCache();
        }
        else
        {
            $handle = fopen($this->cached_file, "r");
            $contents = fread($handle, filesize($this->cached_file));
            fclose($handle);
            if(substr($contents,0,10) == '<!--ONL-->')
            {
                $this->requires_session = true;
            }
        }

    }

    function render() {
        include($this->cached_file);
    }
}
?>
